package xyz.xtgl.erp.project.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import common.core.util.CommonUtil;
import common.core.util.Result;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import java.util.Map;

import plus.logic.LogicBaseController;
import xyz.xtgl.erp.project.entity.BProject;
import xyz.xtgl.erp.project.service.IBProjectService;

/**
 * <p>
 * 基础表 前端控制器
 * </p>
 *
 * @author liubo
 * @since 2020-02-09
 */
@Slf4j
@Controller
@RequestMapping("/project/b_project")
public class BProjectController {

    @Autowired
    private IBProjectService iBProjectService;

    /**
     * 跳转的列表页面
     * @return 跳转页面
     */
    @RequestMapping("/listPage")
    public Object listPage() {
      return "bProject_list";
    }

    /**
     * 列表页面表格数据
     * @param query 查询条件
     * @param page 分页对象
     * @return page 分页对象
     */
    @ResponseBody
    @RequestMapping("/listData")
    public Object listData(QueryWrapper<BProject> query, Page<BProject> page) {
        return Result.success(iBProjectService.page(page, query));
    }

    /**
     * 跳转到修改页面
     * @param id 唯一主键
     * @param model Spring提供的绑定器
     * @return 修改页面
     */
    @RequestMapping("/editPage")
    public Object editPage(String id, Model model) {
        if(CommonUtil.isNotEmpty(id)) {
            BProject entity = this.iBProjectService.getById(id);
            if(CommonUtil.isNotEmpty(entity)) {
                model.addAttribute("entity",entity);
            }
        }
        return "bProject_edit";
    }

    /**
     * 数据保存
     * @param entity 实体类
     * @return Result 统一结果对象
     */
    @ResponseBody
    @RequestMapping("/save")
    public Object save(BProject entity) {
        log.info("开始保存数据:{}" ,entity);
        this.iBProjectService.saveOrUpdate(entity);
        return Result.success();
    }

    /**
     * 数据删除
     * @param queryWrapper 删除条件
     * @return Result 统一结果对象
     */
    @ResponseBody
    @RequestMapping("/removed")
    public Object removed(QueryWrapper<BProject> queryWrapper) {
        this.iBProjectService.remove(queryWrapper);
        return Result.success();
    }

}
